<?php

//include_once "database.php";
include_once "message.php";

class Message_Dao {

	var $db;
	var $message = array();
	
	
	function __construct(Database $db){
		
		$this->db=$db;
		
	}
	
	function add_message (){
		
		$this->db->connect->connect();
		
		if($this->db->table_Exists('message')){
		
			$this->query = "
							INSERT
							INTO message
							(
								sender,
								email,
								subject,
								content
							)
							VALUES
							(
								'".$message->sender."',
								'".$message->email."',
								'".$message->subject."',
								'".$message->content."'
							)
							";
			
			mysql_query($this->query);
			$this->db->connect->disconnect();
		
		}else{
			
			$this->error = "data tidak ditemukan";
			return $this->error;
		
		}
		
	}
	
	function get_message($id=null, $posisi=null, $batas=null){
	
		$this->db->connect->connect();
		
		if($this->db->table_Exists('message')){
		
			if($id!=null AND $posisi==null AND $batas==null){
                $this->query = "SELECT
								*
								FROM
								message
								WHERE 
								message_id = '".$id."'
								";
            }else if($id!=null AND !is_null($posisi) AND !is_null($batas)){
                $this->query = "SELECT
								*
								FROM
								message
								WHERE 
								message_id = '".$id."'
					
								LIMIT $posisi,$batas";
						
            }else if(!is_null($posisi) AND !is_null($batas)){
                $this->query = "SELECT
								*
								FROM
								message
								
								LIMIT $posisi,$batas";
            }else {
                $this->query = "SELECT
								*
								FROM
								message      
								";
            }
            
            
            $result = mysql_query($this->query);  
            
            if($result){
                
                while($r=mysql_fetch_array($result)){
                    
                    $message = new Message();
                    
                    $message->message_id = $r['message_id'];
                    $message->sender = $r['sender'];
                    $message->email = $r['email'];
                    $message->subject = $r['subject'];
                    $message->content = $r['content'];
                                       
                    $this->message[] = $message;
                    
                }
                
                $this->db->connect->disconnect();
            
                return $this->message;
            }else{
                
                $this->error = 'data tidak ditemukan';
                return $this->error;
                
            } 
		
		}else{
            
            $this->error = 'data tidak ditemukan';
            return $this->error;
            
        }           
	
	}

}

?>